//
//  GPDetailViewController.m
//  gupiao
//
//  Created by mac on 2019/8/15.
//  Copyright © 2019 mac. All rights reserved.
//

#import "GPDetailViewController.h"
#import "GPDetailModel.h"
#import "DGPUtil.h"


@interface GPDetailViewController ()
@property (weak, nonatomic) IBOutlet UILabel *titleLab;
@property (weak, nonatomic) IBOutlet UILabel *date;
@property (weak, nonatomic) IBOutlet UIImageView *img;
@property (weak, nonatomic) IBOutlet UILabel *content;
@property (weak, nonatomic) IBOutlet UIButton *collectBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imgHeight;

@end

@implementation GPDetailViewController

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLab.text = self.model.title;
    self.date.text = [NSString stringWithFormat:@"%@ %@",self.model.date,@""];
    self.content.text = self.model.content;
    self.img.image = [UIImage imageNamed:self.model.img];
    
    CGFloat height = self.img.image.size.height;
    self.imgHeight.constant = (height/self.img.image.size.width)*(SCREEN_WIDTH-24);
    NSMutableArray *muArr = [NSMutableArray arrayWithArray:[USER_DEFAULTS objectForKey:[NSString stringWithFormat:@"%@_collect",[DGPUtil getCurrentUserID]]]];
    if ([muArr containsObject:self.model.Id]) {
        self.collectBtn.selected = YES;
    }else{
        self.collectBtn.selected = NO;
    }
}


- (IBAction)collectAction:(id)sender {
    self.collectBtn.selected = !self.collectBtn.selected;
    if (self.collectBtn.selected) {
        NSMutableArray *muArr = [NSMutableArray arrayWithArray:[USER_DEFAULTS objectForKey:[NSString stringWithFormat:@"%@_collect",[DGPUtil getCurrentUserID]]]];
        [muArr addObject:self.model.Id];
        NSArray *array = muArr;
        [USER_DEFAULTS setObject:array forKey:[NSString stringWithFormat:@"%@_collect",[DGPUtil getCurrentUserID]]];
    }else{
        NSMutableArray *muArr = [NSMutableArray arrayWithArray:[USER_DEFAULTS objectForKey:[NSString stringWithFormat:@"%@_collect",[DGPUtil getCurrentUserID]]]];
        [muArr removeObject:self.model.Id];
        NSArray *array = muArr;
        [USER_DEFAULTS setObject:array forKey:[NSString stringWithFormat:@"%@_collect",[DGPUtil getCurrentUserID]]];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
